<template>
  <el-drawer
    @close="close"
    :visible.sync="dialogVisible"
    :title="title"
    :size="500"
  >
    <el-row :gutter="10" class="box-card">
      <el-form ref="elForm" :model="formData" :rules="rules" size="small" label-width="80px">
        <el-col :span="24">
          <el-form-item label="角色名称" prop="name">
            <el-input v-model="formData.name" placeholder="请输入角色名称" clearable :style="{width: '100%'}">
            </el-input>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="描述" prop="intro">
            <el-input
              type="textarea"
              :rows="4"
              placeholder="请输入角色描述"
              v-model="formData.intro"
            >
            </el-input>
          </el-form-item>
        </el-col>
      </el-form>
    </el-row>
    <div class="footer">
      <el-button @click="close">取消</el-button>
      <el-button type="primary" @click="handelConfirm">确定</el-button>
    </div>
  </el-drawer>
</template>
<script>
import { saveRoleSave } from '@/api/roles'
export default {
  props: [],
  data () {
    return {
      dialogVisible: false,
      formData: {
        name: null, // 用户名
        status: '1',
        intro: ''
      },
      rules: {
        name: [{
          required: true,
          message: '请输入角色名称',
          trigger: 'blur'
        }]
      },
      statusOptions: [{
        label: '正常',
        value: '1'
      }, {
        label: '禁用',
        value: '0'
      }],
      deptOptions: [],
      rolesOptions: [],
      title: '新增角色',
      id: null,
      isEdit: false
    }
  },
  computed: {
  },
  watch: {
  },
  mounted () {
  },
  methods: {
    /**
     *
     * @param {*} isEdit 是否编辑
     * @param {*} data 编辑时需要的数据
     */
    async isShow (data, isEdit = false) {
      this.dialogVisible = true
      this.title = isEdit ? '编辑角色' : '新增角色'
      this.isEdit = isEdit
      await this.$nextTick()
      if (isEdit) {
        const formData = this.formData
        for (const key in formData) {
          formData[key] = data[key]
        }
        this.id = data.id
      }
    },
    close () {
      this.$refs.elForm.resetFields()
      this.dialogVisible = false
    },
    handelConfirm () {
      this.$refs.elForm.validate(async valid => {
        if (!valid) return
        this.isEdit ? (await saveRoleSave({ ...this.formData, id: this.id })) : (await saveRoleSave(this.formData))
        this.$notify.success({
          title: `完成${this.title}`
        })
        this.$emit('update:visible')
        this.close()
      })
    }
  }
}

</script>
<style lang="scss" scoped>
.box-card {
  margin-bottom: 10px;
  padding-right: 20px;
  width: 100%;
  padding-left: 10px;
}
.footer {
  display: flex;
  justify-content: space-around;
  margin-bottom: 40px;
  padding-left: 90px;
  padding-right: 20px;
  .el-button {
    width: 100%;
  }
}
</style>
